﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Team4Mock.Entity;
namespace Team4Mock.FormView
{
    public partial class Contact : System.Web.UI.Page
    {
        ContactEntity ce = new ContactEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pnList.Visible = true;
                pnAdd.Visible = false;
                pnAmend.Visible = false;
                gvContact.DataSource = ce.GetDataTrue();
                gvContact.DataBind();
            }
        }
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            pnList.Visible = false;
            pnAdd.Visible = false;
            pnAmend.Visible = true;
            DropDownList1.DataSource = ce.GetContactType();
            DropDownList1.DataBind();
            DropDownList2.DataSource = ce.GetContactBestMethod();
            DropDownList2.DataBind();
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if (Request.Form["select"] != null)
            {
                dlContactType.DataSource = ce.GetContactType();
                dlContactType.DataBind();
                dlBestContactMethod.DataSource = ce.GetContactBestMethod();
                dlBestContactMethod.DataBind();
                ce.ContactID = int.Parse(Request.Form["select"]);
                DataTable dt = new DataTable();
                dt = ce.GetbyID(ce);
                txtFName.Text = dt.Rows[0][1].ToString();
                txtSName.Text = dt.Rows[0][2].ToString();
                txtKnowsAs.Text = dt.Rows[0][3].ToString();
                txtOffPhone.Text = dt.Rows[0][4].ToString();
                txtMobPhone.Text = dt.Rows[0][5].ToString();
                
                NameContact.Value = dt.Rows[0][1].ToString() + " " + dt.Rows[0][2].ToString();
                idContact.Value = dt.Rows[0][0].ToString();
                
                txtHomePhone.Text = dt.Rows[0][6].ToString();
                txtEmail.Text = dt.Rows[0][7].ToString();
                dlContactType.SelectedValue = dt.Rows[0][9].ToString();
                dlBestContactMethod.SelectedValue = dt.Rows[0][10].ToString();
                txtJobRole.Text = dt.Rows[0][11].ToString();
                txtWorkbase.Text = dt.Rows[0][12].ToString();
                txtJobTitle.Text = dt.Rows[0][13].ToString();
                if (dt.Rows[0][14].ToString().Equals("1"))
                    CheckBox1.Checked = true;
                else CheckBox1.Checked = false;
                Session["ContactID"] = Request.Form["select"];
                pnAdd.Visible = true;
                pnList.Visible = false;
                pnAmend.Visible = false;
                None.Checked = false;
                tb.Text = "";
                mess.Text = "";
            }
            else
                tb.Text = "Select 1 records";

        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            pnAdd.Visible = false;
            pnList.Visible = true;
            pnAmend.Visible = false;
            gvContact.DataSource = ce.GetDataTrue();
            gvContact.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtFName.Text == "" || txtSName.Text == "")
            {
                mess.Text = "Input First Name and Surname";
            }
            else
            {
                try
                {
                    ce.FirstName = txtFName.Text.Trim();
                    ce.Suname = txtSName.Text.Trim();
                    ce.KnowAs = txtKnowsAs.Text.Trim();
                    ce.OfficePhone = txtOffPhone.Text.Trim();
                    ce.MobilePhone = txtMobPhone.Text.Trim();
                    ce.STHomePhone = txtHomePhone.Text.Trim();
                    ce.Email = txtEmail.Text.Trim();
                    if (None.Checked == true)
                    {
                        ce.ManagerID = 0;
                    }
                    else
                    {
                        if (idContact.Value != "")
                        {
                            ce.ManagerID = int.Parse(idContact.Value);
                        }
                    }
                    ce.ContactID = int.Parse(Session["ContactID"].ToString());

                    ce.JobRole = txtJobRole.Text.Trim();
                    ce.Workbase = txtWorkbase.Text.Trim();
                    ce.JobTitle = txtJobTitle.Text.Trim();
                    ce.ContactType = int.Parse(dlContactType.SelectedValue);
                    ce.BestContactMethod = int.Parse(dlBestContactMethod.SelectedValue);
                    if (CheckBox1.Checked == true) ce.IsActive = 1;
                    else ce.IsActive = 0;
                    ce.UpdateData(ce);
                    mess.Text = "Update success";
                    Session.Remove("ContactID");
                }
                catch
                {
                    mess.Text = "Error";
                }
            }
        }
        public SortDirection SortDirection
        {
            get
            {
                if (ViewState["SortDirection"] == null)
                {
                    ViewState["SortDirection"] = SortDirection.Ascending;
                }
                return (SortDirection)ViewState["SortDirection"];
            }
            set
            {
                ViewState["SortDirection"] = value;
            }
        }

        protected void gvContact_Sorting(object sender, GridViewSortEventArgs e)
        {
            tb.Text = "";
            DataTable dt = new DataTable();
            if (ckActive.Checked == true)
            {
                dt = ce.GetData();
            }
            else
                dt = ce.GetDataTrue();

            string sortExpression = e.SortExpression;
            string direction = string.Empty;

            if (SortDirection == SortDirection.Ascending)
            {
                SortDirection = SortDirection.Descending;
                direction = " DESC";
            }
            else
            {
                SortDirection = SortDirection.Ascending;
                direction = " ASC";
            }
            dt.DefaultView.Sort = sortExpression + direction;

            gvContact.DataSource = dt;
            gvContact.DataBind();
        }

        protected void ckActive_CheckedChanged(object sender, EventArgs e)
        {
            if (ckActive.Checked == true)
            {
                gvContact.DataSource = ce.GetData();
            }
            else { gvContact.DataSource = ce.GetDataTrue(); }
            gvContact.DataBind();
            tb.Text = "";
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtFirstName.Text.Trim() != "" || txtSurname.Text.Trim() != "")
            {
                int index = 0;
                if (txtFirstName.Text.Trim() == "" && txtSurname.Text.Trim() != "")
                    index = 2;
                if (txtSurname.Text.Trim() == "" && txtFirstName.Text.Trim() != "")
                    index = 1;
                if (txtFirstName.Text.Trim() != "" && txtSurname.Text.Trim() != "")
                    index = 3;
                ce.FirstName = txtFirstName.Text.Trim();
                ce.Suname = txtSurname.Text.Trim();
                ce.Index = index;
                gvContact.DataSource = ce.Search(ce);
                gvContact.DataBind();
                if (gvContact.Rows.Count == 0)
                    tb.Text = "No result";
                else
                    tb.Text = gvContact.Rows.Count.ToString() + " result";
            }
            else
                tb.Text = "Input FirstName or Surname";
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtFirstName.Text = "";
            txtSurname.Text = "";
            txtFirstName.Focus();
            tb.Text = "";
            gvContact.DataSource = ce.GetDataTrue();
            gvContact.DataBind();
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "" || TextBox2.Text == "")
            {
                lb.Text = "Input First Name and Surname";
            }
            else
            {
                ce.FirstName = TextBox1.Text.Trim();
                ce.Suname = TextBox2.Text.Trim();
                ce.KnowAs = TextBox3.Text.Trim();
                ce.OfficePhone = TextBox4.Text.Trim();
                ce.MobilePhone = TextBox6.Text.Trim();
                ce.STHomePhone = TextBox8.Text.Trim();
                ce.Email = TextBox10.Text.Trim();
                if (ckNone.Checked == true)
                {
                    ce.ManagerID = 0;
                }
                else
                {
                    if (IDNew.Value == "")
                        ce.ManagerID = 0;
                    else
                        ce.ManagerID = int.Parse(IDNew.Value);
                }
                ce.JobRole = TextBox5.Text.Trim();
                ce.Workbase = TextBox7.Text.Trim();
                ce.JobTitle = TextBox9.Text.Trim();
                ce.ContactType = int.Parse(DropDownList1.SelectedValue);
                ce.BestContactMethod = int.Parse(DropDownList2.SelectedValue);
                if (CheckBox2.Checked == true) ce.IsActive = 1;
                else ce.IsActive = 0;
                ce.InsertData(ce);
                lb.Text = "Insert success";
            }
        }

    }
}